import { useDict } from '@/utils/dict'
import { download } from '@/utils/request'
import { parseTime, resetForm, handleTree, addDateRange, selectDictLabel, selectDictLabels } from '@/utils/ruoyi'
import { getConfigKey } from "@/api/system/config"

// 分页组件
import Pagination from '@/components/Pagination'
// 自定义表格工具组件
import RightToolbar from '@/components/RightToolbar'
// 富文本组件
import Editor from "@/components/Editor"
// 文件上传组件
import FileUpload from "@/components/FileUpload"
// 图片上传组件
import ImageUpload from "@/components/ImageUpload"
// 图片预览组件
import ImagePreview from "@/components/ImagePreview"
// 字典标签组件
import DictTag from '@/components/DictTag'

import { Icon } from '@iconify/vue';

import ContentWrap from "@/components/ContentWrap";
import * as elIcons from '@element-plus/icons-vue'


import SvgIcon from '@/components/SvgIcon'


import hasRole from './directive/permission/hasRole'
import hasPermi from './directive/permission/hasPermi'
import copyText from './directive/common/copyText'


import time from "./directive/time.js";
import copy from "./directive/copy.js";

// 注册指令
import $tab from './plugins/tab'
import $auth from './plugins/auth'
import $cache from './plugins/cache'
import $modal from './plugins/modal'
import $download from './plugins/download'

import errorHandler from "./utils/errorHandler.js";


export default {
  install(app) {
    // 全局方法挂载
    app.config.globalProperties.useDict = useDict
    app.config.globalProperties.download = download
    app.config.globalProperties.parseTime = parseTime
    app.config.globalProperties.resetForm = resetForm
    app.config.globalProperties.handleTree = handleTree
    app.config.globalProperties.addDateRange = addDateRange
    app.config.globalProperties.getConfigKey = getConfigKey
    app.config.globalProperties.selectDictLabel = selectDictLabel
    app.config.globalProperties.selectDictLabels = selectDictLabels

    // 页签操作
    app.config.globalProperties.$tab = $tab
    // 认证对象
    app.config.globalProperties.$auth = $auth
    // 缓存对象
    app.config.globalProperties.$cache = $cache
    // 模态框对象
    app.config.globalProperties.$modal = $modal
    // 下载文件
    app.config.globalProperties.$download = $download

    // 全局组件挂载
    app.component('DictTag', DictTag)
    app.component('Pagination', Pagination)
    app.component('FileUpload', FileUpload)
    app.component('ImageUpload', ImageUpload)
    app.component('ImagePreview', ImagePreview)
    app.component('RightToolbar', RightToolbar)
    app.component('Editor', Editor)
    app.component('ContentWrap', ContentWrap)
    app.component('SvgIcon', SvgIcon)
    app.component('Icon', Icon)


    //统一注册el-icon图标
    for (let key in elIcons) {
      const componentConfig = elIcons[key]
      // app.component(`ElIcon${icon}`, elIcons[key]);
      app.component(componentConfig.name, componentConfig)
    }


    //注册全局指令
    app.directive('hasRole', hasRole)
    app.directive('hasPermi', hasPermi)
    app.directive('copyText', copyText)


    app.directive("time", time);
    app.directive("copy", copy);

    //关闭async-validator全局控制台警告
    window.ASYNC_VALIDATOR_NO_WARNING = 1;

    //全局代码错误捕捉
    app.config.errorHandler = errorHandler;

  }
}
